package org.example.hospital_registration_system.utils;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;

public class SecurityUtil {
    /*
    * 进行加密操作(验证+密文)
    */
    public static String encrypt(String password){
        String salt = UUID.randomUUID().toString().replace("-","");
        System.out.println(salt);
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
//        最终密码 md5（（盐值+md5密码））+盐值
        String finalPassword = DigestUtils.md5DigestAsHex((md5Password+salt).getBytes());
        return finalPassword+salt;
    }

//    public static void main(String[] args) {
//        System.out.println(encrypt("123"));
//    }


    /*
    * 校验
    * */
    public static boolean verify(String password,String finalPassword){
        if(!StringUtils.hasLength(password)){
            return false;
        }
        //获取盐值
        String salt = finalPassword.substring(32,64);

        //对用户密码进行加密
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        String finalPassword1 = DigestUtils.md5DigestAsHex((md5Password+salt).getBytes());

        //验证
        if((finalPassword1+salt).equals(finalPassword)){
            return true;
        }else {
            return false;
        }
    }
}
